<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店名字" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入门店名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入门店地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnabled">
        <el-select v-model="queryParams.isEnabled" placeholder="请选择是否启用" clearable size="small">
          <el-option
            v-for="dict in dict.type.store_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pms:store:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pms:store:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pms:store:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pms:store:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="网点id" align="center" prop="storeId" v-if="false"/>
      <el-table-column label="门店地址" align="left" prop="address"/>
      <el-table-column label="地图位置" align="center" prop="position">
        <template slot-scope="scope">
          <el-button type="primary" @click="showMap(scope)">查看地图</el-button>
          <StoreMap ref="StoreMap"/>
        </template>
      </el-table-column>
      <el-table-column label="门店名字" align="center" prop="storeName"/>
      <el-table-column label="门店负责人" align="center" prop="managerId"/>
      <el-table-column label="联系电话" align="center" prop="telephone"/>
      <el-table-column label="联系邮箱" align="center" prop="email"/>
      <el-table-column label="营业开始时间" align="center" prop="openHour">
        <template slot-scope="scope">
          <span>PM  {{ scope.row.openHour }}</span>
        </template>
      </el-table-column>
      <el-table-column label="营业结束时间" align="center" prop="closeHour">
        <template slot-scope="scope">
          <span>PM  {{ scope.row.closeHour }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="isEnabled">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.store_enable" :value="scope.row.isEnabled"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="info"
            icon="el-icon-thumb"
            @click="handleDetail(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cms:car:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:car:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="pagination.total>0"
      :total="pagination.total"
      :page.sync="pagination.current"
      :limit.sync="pagination.size"
      @pagination="getPage"
    />
    <!-- 添加或修改线下网点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="门店id" prop="storeId" v-if="false">
          <el-input v-model="form.storeId" placeholder="请输入门店"/>
        </el-form-item>
        <!--        <el-form-item label="所在城市id" prop="cityId">-->
        <!--          <el-input v-model="form.cityId" placeholder="请输入所在城市id"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="门店负责人" prop="managerId">
          <el-select v-model="form.managerId" placeholder="请选择门店店长" filterable size="medium">
            <el-option
              v-for="manager in managerList"
              :key="manager.userId"
              :label="manager.nickName"
              :value="manager.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店地址" prop="position">
          <el-input v-model="form.address" placeholder="请输入门店详细地址"/>
        </el-form-item>
        <el-form-item label="经纬度" prop="position">
          <el-input v-model="form.position" placeholder="请输入经纬度"/>
        </el-form-item>
        <!--        //选怎坐标-->
        <el-form-item label="选择地图位置" prop="position">
          <SearchPoi @getPosition="form.position=$event" ref="searchpoi" :store-position="form.position" />
        </el-form-item>
        <el-form-item label="门店名字" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入门店名字"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="联系邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入联系邮箱"/>
        </el-form-item>
        <el-form-item label="营业开始时间" prop="openHour">
          <el-date-picker clearable size="small"
                          v-model="form.openHour"
                          type="datetime"
                          value-format="HH:mm:ss"
                          placeholder="请选择营业开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="营业结束时间" prop="closeHour">
          <el-date-picker clearable size="small"
                          v-model="form.closeHour"
                          type="datetime"
                          value-format="HH:mm:ss"
                          placeholder="请选择营业结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnabled">
          <el-select v-model="form.isEnabled" placeholder="请选择是否启用">
            <el-option
              v-for="dict in dict.type.store_enable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listStore, getStore, delStore, addStore, updateStore, pageStore } from '@/api/pms/store'
import StoreMap from '@/views/pms/store/storeMap'
import SearchPoi from '@/components/Amap/searchPoi'
import { listUser } from '@/api/system/user'
export default {
  name: 'Store',
  components: { StoreMap, SearchPoi },
  dicts: ['store_enable'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线下网点表格数据
      storeList: [],
      //负责人列表
      managerList:[],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      // 查询参数,
      searchParams: {
        queryParams: []
      },
      pagination: {
        current: 1, // 当前页
        size: 10, // 每页显示多少条
        total: 1 // 总数
      },
      queryParams: {
        storeName: undefined,
        telephone: undefined,
        address: undefined,
        isEnabled: undefined
      },
      option_map: {
        mapLoad: true
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getPage()
    this.initAMap()
  },
  methods: {
    /** 查询线下网点列表 */
    getPage() {
      this.loading = true
      const params = Object.assign({}, this.pagination, { queryParams: JSON.stringify(this.searchParams.queryParams) })
      pageStore(params).then(response => {
        this.storeList = response.data.list
        this.pagination.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        storeId: undefined,
        cityId: undefined,
        managerId: undefined,
        longitude: undefined,
        latitude: undefined,
        position: undefined,
        storeName: undefined,
        telephone: undefined,
        email: undefined,
        address: undefined,
        openHour: undefined,
        closeHour: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        deletedId: undefined,
        deletedTime: undefined,
        isDeleted: undefined,
        isEnabled: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.current = 1
      this.searchParams.queryParams = []
      console.log('items', this.queryParams)
      const queryParams = this.queryParams
      for (let key in queryParams) {
        console.log('key', queryParams[key])
        if (queryParams[key] !== undefined) {
          this.searchParams.queryParams.push({
            'columnName': key,
            'selectCondition': 'like',
            'val': [queryParams[key]]
          })
        }
      }
      this.getPage()
    },

    /** 重置钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getManagerList()
      this.open = true
      this.title = '添加线下网点'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const storeId = row.storeId || this.ids
      this.getManagerList()
      getStore(storeId).then(response => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改线下网点'
        this.$refs.searchpoi.storePosition=this.form.position
        this.$refs.searchpoi.updateMap()
      })
      // this.$refs.searchpoi.updateMap()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.storeId != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getPage()
            }).finally(() => {
              this.buttonLoading = false
            })
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getPage()
            }).finally(() => {
              this.buttonLoading = false
            })
          }
        }
      })
    },
    initAMap() {

    },
    mapLoad() {
      this.handleUpdate()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeIds = row.storeId || this.ids
      this.$modal.confirm('是否确认删除线下网点编号为"' + storeIds + '"的数据项？').then(() => {
        this.loading = true
        return delStore(storeIds)
      }).then(() => {
        this.loading = false
        this.getPage()
        this.$modal.msgSuccess('删除成功')
      }).finally(() => {
        this.loading = false
      })
    },
    showMap(data) {
      this.$nextTick(() => {
        this.$refs.StoreMap.show(true, data)
      })
    },
    getManagerList(){
      listUser().then((res)=>{
        this.managerList=res.rows
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pms/store/export', {
        ...this.queryParams
      }, `store_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
